package com.aistar.service.impl;

import com.aistar.mapper.WorkExperienceMapper;
import com.aistar.pojo.WorkExperience;
import com.aistar.pojo.WorkExperienceExample;
import com.aistar.service.WorkExperienceService;
import com.aistar.util.MyBatisUtil;
import com.aistar.util.ServerResponse;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class WorkExperienceServiceImpl implements WorkExperienceService {
    @Override
    public ServerResponse save(WorkExperience workExperience) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        WorkExperienceMapper mapper =   sqlSession.getMapper(WorkExperienceMapper.class);
        int rows = mapper.insert(workExperience);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession();
        if(rows >0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    //获得到当前用户的某个简历中最新添加的工作经验
    public ServerResponse getLatestByResumeId(Integer resumeId){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        WorkExperienceMapper mapper =   sqlSession.getMapper(WorkExperienceMapper.class);
        WorkExperienceExample experienceExample = new WorkExperienceExample();

        experienceExample.createCriteria().andResumeIdEqualTo(resumeId).andStatusEqualTo(0);
        experienceExample.setOrderByClause("work_experi_id desc");

        WorkExperience experience = mapper.selectByExample(experienceExample).get(0);
        return ServerResponse.getDataSuccess(experience);
    }

    @Override
    public ServerResponse modifyStatus(WorkExperience workExperience) {
        return null;
    }

    @Override
    public ServerResponse modify(WorkExperience workExperience) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        WorkExperienceMapper mapper =   sqlSession.getMapper(WorkExperienceMapper.class);
        int rows = mapper.updateByPrimaryKeySelective(workExperience);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession();
        if(rows >0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();

    }

    @Override
    public ServerResponse getById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        WorkExperienceMapper mapper =   sqlSession.getMapper(WorkExperienceMapper.class);
        WorkExperience experience = mapper.selectByPrimaryKey(id);
        MyBatisUtil.closeSqlSession();
        if(experience !=null)
            return ServerResponse.getDataSuccess(experience);
        return ServerResponse.getDataFailed("没有该工作经验");
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }


    public ServerResponse getAllByResumeId(Integer resumeId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        WorkExperienceMapper mapper =   sqlSession.getMapper(WorkExperienceMapper.class);
        WorkExperienceExample experienceExample = new WorkExperienceExample();

        experienceExample.createCriteria().andResumeIdEqualTo(resumeId).andStatusEqualTo(0);

       List<WorkExperience> workExperienceList =  mapper.selectByExample(experienceExample);
       if(workExperienceList!=null && workExperienceList.size() >0)
           return ServerResponse.getDataSuccess(workExperienceList);
        return ServerResponse.getDataFailed("暂无工作经验");
    }
}
